
package com.hazelcast.internal.util;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@FunctionalInterface
public interface NetworkInterfacesEnumerator {
    static NetworkInterfacesEnumerator defaultEnumerator() {
        return new DefaultNetworkInterfacesEnumerator();
    }

    List<NetworkInterfaceInfo> getNetworkInterfaces() throws SocketException;
}

class DefaultNetworkInterfacesEnumerator implements NetworkInterfacesEnumerator {

    @Override
    public List<NetworkInterfaceInfo> getNetworkInterfaces() throws SocketException {
        List<NetworkInterfaceInfo> infos = new ArrayList<>();
        for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            NetworkInterfaceInfo networkInterfaceInfo = new NetworkInterfaceInfo(networkInterface);
            infos.add(networkInterfaceInfo);
        }
        return infos;
    }
}
